<template>
    <div class="main-container">
        <!-- 历史记录侧边栏 -->
        <div class="sidebar">
            <div class="history-header">
                <h3>对话历史</h3>
                <div class="decorative-line"></div>
            </div>
            <div class="history-list">
                <div v-for="(item, index) in history" :key="index" class="history-item" @click="loadHistory(item)">
                    <div class="history-title">{{ item.title }}</div>
                    <div class="history-time">{{ formatDate(item.time) }}</div>
                </div>
            </div>
        </div>

        <!-- 聊天主区域 -->
        <div class="chat-container">
            <!-- 动态背景层 -->
            <div class="dynamic-bg">
                <div class="grid-lines"></div>
                <div class="glow-effect"></div>
            </div>

            <!-- 聊天内容 -->
            <div class="chat-content">
                <div class="chat-box" ref="chatBox">
                    <div v-for="(msg, index) in messages" :key="index" :class="['message', msg.from]">
                        <div class="message-content">
                            <div class="message-bubble">
                                <p>{{ msg.text }}</p>
                            </div>
                            <div class="message-time">{{ formatTime(msg.time) }}</div>
                        </div>
                    </div>
                </div>

                <!-- 输入区域 -->
                <div class="input-area">
                    <div class="input-wrapper">
                        <input v-model="userInput" type="text" placeholder="向Ace AI提问..." @keyup.enter="sendMessage"
                            class="neon-input" :disabled="isLoading" />
                        <button @click="sendMessage" class="neon-button" :disabled="isLoading">
                            <span class="send-text">{{ isLoading ? '生成中...' : '发送' }}</span>
                            <div class="neon-effect"></div>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            userInput: '',
            messages: [
                {
                    text: '你好，我是专业的数智旅游知识智能体，你可以试着向我询问旅游方面的问题，悄悄告诉你，我对成都跟熟悉喔。试着输入：介绍成都的景点。看看我会怎么回复吧',
                    from: 'ai', time: new Date()
                },
            ],
            isLoading: false,
            tempMessageId: null,
            history: [
                {
                    title: '首次对话',
                    time: new Date(),
                    messages: [{ text: '你好...', from: 'ai', time: new Date() }]
                }
            ]
        };
    },
    methods: {
        formatTime(time) {
            if (!time) return '';
            const date = new Date(time);
            return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
        },
        formatDate(time) {
            return new Date(time).toLocaleDateString()
        },
        sendMessage() {
            if (!this.userInput.trim() || this.isLoading) return;

            const userMessage = {
                text: this.userInput,
                from: 'user',
                time: new Date()
            };
            this.messages.push(userMessage);

            const question = this.userInput;
            this.userInput = '';
            this.isLoading = true;

            const thinkingMessage = {
                text: 'Ace AI正在思考中...',
                from: 'ai',
                time: new Date(),
                tempId: Date.now()
            };
            this.messages.push(thinkingMessage);
            this.tempMessageId = thinkingMessage.tempId;

            this.$axios.post('/api/chat/question', { question })
                .then(response => {
                    this.removeTempMessage();
                    if (response.data.code == 200) {
                        const aiMessage = {
                            text: response.data.data,
                            from: 'ai',
                            time: new Date()
                        };
                        this.messages.push(aiMessage);
                        this.updateHistory();
                    } else {
                        this.$message.error('请求失败: ' + response.data.msg);
                    }
                })
                .catch(error => {
                    this.removeTempMessage();
                    console.error("API请求失败", error);
                    this.$message.error('服务暂时不可用');
                })
                .finally(() => {
                    this.isLoading = false;
                    this.scrollToBottom();
                });
        },
        removeTempMessage() {
            if (this.tempMessageId) {
                this.messages = this.messages.filter(
                    msg => msg.tempId !== this.tempMessageId
                );
                this.tempMessageId = null;
            }
        },
        updateHistory() {
            if (this.messages.length > 1) {
                this.history.unshift({
                    title: this.messages[1].text.substring(0, 20) + '...',
                    time: new Date(),
                    messages: [...this.messages]
                })
            }
        },
        loadHistory(item) {
            this.messages = [...item.messages]
        },
        scrollToBottom() {
            this.$nextTick(() => {
                this.$refs.chatBox.scrollTop = this.$refs.chatBox.scrollHeight;
            });
        }
    }
};
</script>

<style scoped>
.main-container {
    display: flex;
    height: 90vh;
    background: #0a192f;
}

.sidebar {
    width: 280px;
    background: rgba(12, 25, 47, 0.95);
    border-right: 1px solid rgba(74, 144, 226, 0.1);
    backdrop-filter: blur(8px);
    padding: 20px;
}

.history-header {
    margin-bottom: 25px;
}

.history-header h3 {
    color: #64ffda;
    font-size: 1.2em;
    margin-bottom: 10px;
}

.decorative-line {
    height: 2px;
    background: linear-gradient(90deg, transparent, #64ffda 50%, transparent);
}

.history-list {
    max-height: calc(80vh - 80px);
    overflow-y: auto;
}

.history-item {
    padding: 12px;
    margin: 8px 0;
    background: rgba(23, 42, 69, 0.6);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.history-item:hover {
    background: rgba(74, 144, 226, 0.15);
    transform: translateX(5px);
}

.history-title {
    color: #8892b0;
    font-size: 0.9em;
    line-height: 1.4;
}

.history-time {
    color: #495670;
    font-size: 0.75em;
    margin-top: 4px;
}

.chat-container {
    flex: 1;
    position: relative;
    background: #0a192f;
}

.dynamic-bg {
    position: absolute;
    width: 80%;
    height: 80%;
    background:
        radial-gradient(circle at 50% 50%,
            rgba(100, 255, 218, 0.05) 0%,
            transparent 70%);
}

.grid-lines {
    position: absolute;
    width: 80%;
    height: 80%;
    background-image:
        linear-gradient(rgba(74, 144, 226, 0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(74, 144, 226, 0.03) 1px, transparent 1px);
    background-size: 20px 20px;
}

.glow-effect {
    position: absolute;
    width: 100%;
    height: 100%;
    background: linear-gradient(45deg,
            rgba(100, 255, 218, 0.02) 0%,
            rgba(74, 144, 226, 0.02) 100%);
    animation: glow 8s infinite alternate;
}

@keyframes glow {
    0% {
        opacity: 0.3;
    }

    100% {
        opacity: 0.7;
    }
}

.chat-content {
    position: relative;
    z-index: 1;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.chat-box {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    background: transparent;
}

.message {
    display: flex;
    margin: 18px 0;
    transition: all 0.3s ease;
}

.message.user {
    justify-content: flex-end;
}

.message.ai {
    justify-content: flex-start;
}

.message-bubble {
    max-width: 70%;
    padding: 16px 24px;
    border-radius: 12px;
    position: relative;
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.message.user .message-bubble {
    background: linear-gradient(135deg, rgba(74, 144, 226, 0.9), rgba(34, 102, 210, 0.9));
    color: white;
    border-radius: 12px 12px 4px 12px;
}

.message.ai .message-bubble {
    background: rgba(23, 42, 69, 0.8);
    color: #e2e8f0;
    border-radius: 12px 12px 12px 4px;
    border: 1px solid rgba(100, 255, 218, 0.1);
}

.message-time {
    font-size: 0.8em;
    color: #94a3b8;
    margin-top: 4px;
    padding: 0 8px;
}

.input-area {
    padding: 20px;
    background: rgba(12, 25, 47, 0.9);
    border-top: 1px solid rgba(100, 255, 218, 0.1);
}

.input-wrapper {
    display: flex;
    gap: 12px;
    position: relative;
}

.neon-input {
    flex: 1;
    padding: 14px 24px;
    border: none;
    border-radius: 8px;
    background: rgba(23, 42, 69, 0.6);
    color: #e2e8f0;
    font-size: 16px;
    transition: all 0.3s ease;
    border: 1px solid rgba(100, 255, 218, 0.2);
}

.neon-input:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.2);
}

.neon-button {
    position: relative;
    padding: 14px 32px;
    border: none;
    border-radius: 8px;
    background: linear-gradient(135deg, #0077E6, #0052B3);
    color: white;
    cursor: pointer;
    overflow: hidden;
    transition: all 0.3s ease;
}

.neon-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.3);
}

.neon-effect {
    position: absolute;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg,
            transparent,
            rgba(255, 255, 255, 0.15),
            transparent);
    animation: neonFlow 2s infinite;
}

@keyframes neonFlow {
    0% {
        left: -80%
    }

    100% {
        left: 80%
    }
}

@keyframes messageAppear {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message {
    animation: messageAppear 0.4s cubic-bezier(0.22, 0.61, 0.36, 1);
}

/* 滚动条样式 */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: rgba(11, 16, 26, 0.8);
}

::-webkit-scrollbar-thumb {
    background: #2A4365;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #0066CC;
}
</style>